package thread.forkJoinPool;

import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * User: Administrator
 * Date: 2021/3/10
 * Time: 10:57
 * Description:
 */
public class Task extends RecursiveAction {
    private List<Product> products;
    private int first;
    private int last;
    private double incre;

    public Task(List<Product> products, int first, int last, double incre) {
        this.products = products;
        this.first = first;
        this.last = last;
        this.incre = incre;
    }

    @Override
    protected void compute() {
        if (last - first < 10) {
            updatePrice();
        } else {
            int middle = (last + first) / 2;
            System.out.println("task" + getQueuedTaskCount());
            Task t1 = new Task(products, first, middle + 1, incre);
            Task t2 = new Task(products, middle + 1, last, incre);
            invokeAll(t1, t2);
        }
    }

    private void updatePrice() {
        for (int i = first; i < last; i++) {
            Product product = products.get(i);
            product.setPrice(product.getPrice() * (1 + incre));
        }
    }
}
